package com.etcbase.dcs.controller.map;

import android.database.Cursor;
import android.os.AsyncTask;

import com.etcbase.dcs.model.map.MapNote;

/*
 * Asynch-Task for retrieving all data from the database
 */
public class MapNoteListAsyncTask extends AsyncTask<Object, Object, Cursor> {
	private MapNote mSearchItem;
	private MapNoteDao mMapNoteViewDataBaseConnector;
	private OnGetItemsListener mOnGetItemsListener;

	/*
	 * There is no need to close the database by the implementor
	 */
	public interface OnGetItemsListener {
		public void onGetItems(Cursor cursor);
	}

	public void setOnGetItemsListener(OnGetItemsListener listener) {
		mOnGetItemsListener = listener;
	}

	public MapNoteListAsyncTask(MapNoteDao dbc) {
		mSearchItem = null;
		mMapNoteViewDataBaseConnector = dbc;
	}

	public MapNoteListAsyncTask(MapNoteDao dbc,
			MapNote search) {
		mSearchItem = search;
		mMapNoteViewDataBaseConnector = dbc;
	}

	@Override
	protected Cursor doInBackground(Object... params) {
		mMapNoteViewDataBaseConnector.openForRead();
		return mMapNoteViewDataBaseConnector.get(mSearchItem);
	}

	@Override
	protected void onPostExecute(Cursor result) {
		if (mOnGetItemsListener != null) {
			mOnGetItemsListener.onGetItems(result);
		}
		mMapNoteViewDataBaseConnector.close();
	}

}
